home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest1_0.lha / Tests / barrier / barriertest.C < prev    next >
C/C++ Source or Header  |  1991-12-11  |  2KB  |  124 lines

  1. //
  2. // This program is intended to test the basic parallel "hello world".
  3. //
  4.  
  5. #include "presto.h"
  6.  
  7. #include "barrier.h"
  8.  
  9. Lock coutLock;
  10.  
  11. MasterSlaveBarrier *barrier;
  12.  
  13. #define MAXPROCS  32
  14.  
  15. int blockcount = 10;
  16. int numslaves = 1;
  17.  
  18. void
  19. err(char *s)
  20. {
  21.     cerr << s << "\n";
  22.     exit(1);
  23. }
  24.  
  25. Main::init()
  26. {
  27.     cout << "Initializing\n";
  28.     cout.flush();
  29.     numprocessors = 1;
  30.     quantum = 0;
  31.  
  32.     for (argc--, argv++; *argv && **argv == '-'; argv++, argc--)
  33.         switch (*(*argv + 1)) {
  34.             case 'q':
  35.                 quantum = atoi(*argv + 2);
  36.                 break;
  37.             case 'p':
  38.                 numprocessors = atoi(*argv + 2);
  39.                 break;
  40.             case 'b':
  41.                 blockcount = atoi(*argv + 2);
  42.                 break;
  43.             case 's':
  44.                 numslaves = atoi(*argv + 2);
  45.                 break;
  46.             default:
  47.                 cerr << chr(*(*argv + 1)) << " unknown flag.\n";
  48.                     return -1;
  49.         }
  50.     return 0;
  51. }
  52.  
  53. int
  54. body(int zot)
  55. {
  56. //    int i;
  57. //    for (i = 0; i < 50; i++);
  58.     return zot;
  59. }
  60.  
  61. void
  62. worker(int pid)
  63. {
  64.   int waste;
  65.   int i;
  66.   for (i=0; i<blockcount; i++) {
  67.     coutLock.lock();
  68.     cout << "Hello world (" << pid << "[" << (int)(&pid) << "])\n";
  69.     cout.flush ();
  70.     coutLock.unlock();
  71. //    for (waste=0; waste<1000000+200000*((5-pid+i)%5); waste++) ;
  72.     for (waste=0; waste<100; waste++) ;
  73.       // coutLock.lock();
  74.       // cout << "worker " << pid << " arriving at barrier\n";
  75.       // cout.flush ();
  76.       // coutLock.unlock();
  77.     barrier->SlaveArrive ();
  78.       // coutLock.lock();
  79.       // cout << "worker " << pid << " continuing after barrier\n";
  80.       // cout.flush ();
  81.       // coutLock.unlock();
  82.   }
  83. }
  84.  
  85.  
  86. Main::main()
  87. {
  88.     int i;
  89.     char ch;
  90.  
  91.     Thread *cpus[MAXPROCS];
  92.  
  93.     cout << "Here we go\n";
  94.     cout.flush();
  95.  
  96.         barrier = new MasterSlaveBarrier (numslaves);
  97.  
  98.     cout << "Barrier created!\n";
  99.     cout.flush();
  100.  
  101.     for (i = 0; i < numslaves; i++) {
  102.             char buf[12];
  103.         sprintf( buf, "%s.%d", "SLAVE", i );
  104.         char *name = new char[ strlen(buf) + 1 ];
  105.         strcpy( name, buf );
  106.         cpus[i] = new Thread(name);
  107.         cpus[i]->willjoin();
  108.         cpus[i]->start((Objany)0, (PFany)worker,
  109.                 (i));
  110.     }
  111.  
  112.         for (i=0; i<blockcount; i++) {
  113.           barrier->MasterArrive ();
  114. //          cout << "Waiting for you...";
  115. //          cin >> ch;
  116.           barrier->LetSlavesGo ();
  117.         }
  118.  
  119.     for (i = 0; i < numslaves; i++) {
  120.         cpus[i]->join();
  121.     }
  122.     cout << "...done.\n";
  123. }
  124.